package com.cxs.xiaomi.web.controller;

import com.cxs.xiaomi.domain.Cart;
import com.cxs.xiaomi.domain.Goods;
import com.cxs.xiaomi.domain.User;
import com.cxs.xiaomi.service.CartService;
import com.cxs.xiaomi.service.GoodsService;
import com.cxs.xiaomi.service.impl.CartServiceImpl;
import com.cxs.xiaomi.service.impl.GoodsServiceImpl;
import com.cxs.xiaomi.utils.Page;
import com.cxs.xiaomi.utils.StringUtil;
import cxs.utils.Result;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/9/14  16:13
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@WebServlet(name = "CartServlet", urlPatterns = "/cartservlet")
public class CartServlet extends BaseServlet {

    private CartService cartService = new CartServiceImpl();

    // 添加购物车
    public String addCart(HttpServletRequest request, HttpServletResponse response){
        User user = getUser(request, response);
        if(user != null){
            String goodsId = request.getParameter("goodsId");
            String number = request.getParameter("number");
            if(StringUtil.empty(goodsId)){
                request.setAttribute("msg","商品不能为空");
                return "forward:/message.jsp";
            }
            if(StringUtil.empty(number)){
                request.setAttribute("msg","商品数量不能为空");
                return "forward:/message.jsp";
            }
            Cart cart = new Cart(user.getId(),Integer.parseInt(goodsId.trim()),Integer.parseInt(number.trim()),null);
            if(cartService.addCart(user.getId(),cart)){
                return "redirect:/cartSuccess.jsp";
            }
            request.setAttribute("msg","购物车添加失败");
            return "forward:/message.jsp";
        }else{
            request.setAttribute("msg","登录信息已过期，请登录");
            return "forward:/message.jsp";
        }
    }

    // 获得我的购物车
    public String getCart(HttpServletRequest request, HttpServletResponse response){
        User user = getUser(request, response);
        if(user != null){
            List<Cart> cartList = cartService.getCart(user.getId());
            request.setAttribute("cartList",cartList);
            return "forward:/cart.jsp";
        }
        return "redirect:/login.jsp";
    }

    // 购物车商品数量的添加、减少、删除
    public Object addCartAjax(HttpServletRequest request, HttpServletResponse response){
        User user = getUser(request, response);
        if(user!= null){
            String goodsId = request.getParameter("goodsId");
            String number = request.getParameter("number");
            String price = request.getParameter("price");
            if(StringUtil.empty(goodsId)){
                return Result.failare("goodsId出错");
            }
            if(StringUtil.empty(number)){
                return Result.failare("number出错");
            }
            if(StringUtil.empty(price)){
                return Result.failare("price出错");
            }
            try {
                int num = Integer.parseInt(number);
                int pid = Integer.parseInt(goodsId);
                int money = Integer.parseInt(price);
                if(num == 0){
                    // 删除操作
                    return cartService.delete(user.getId(),pid) ? Result.OK() : Result.failare("操作失败");
                }else{
                    // 修改操作
                    return cartService.update(user.getId(),pid,num,money) ? Result.OK() : Result.failare("操作失败");
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }

            return Result.failare("操作失败");
        }
        return Result.OK("登录信息已过期,请重新登陆",403,null);
    }

    // 清空购物车
    public Object clearCartAjax(HttpServletRequest request, HttpServletResponse response){
        User user = getUser(request, response);
        if(user != null){
            return cartService.clearCart(user.getId()) ? Result.OK("操作成功") : Result.failare("操作失败");
        }
        return Result.OK("登录信息已过期,请重新登陆",403,null);
    }

    // 测试
    public void test(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("测试方法执行");
    }
}
